<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<% String path=request.getContextPath(); String basePath=request.getScheme()+ "://" + request.getServerName() + ":" + request.getServerPort() + path+ "/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html  xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<base href="<%=basePath%>">
<title>跟单费用配置</title>
	<%@ include file="/WEB-INF/jsp/common/common.jsp" %>
	<script src="<%=path%>/js/jquery.divbox.js" type="text/javascript"></script>
	<script src="js/json2.js" type="text/javascript"></script>
	<script type="text/javascript">
	
	//自定义map功能
	function Map(){
	  this.elements = new Array();
	   //获取MAP元素个数
	   this.size = function() {
	       return this.elements.length;
	   };
	   //判断MAP是否为空
	   this.isEmpty = function() {
	       return (this.elements.length < 1);
	   };
	   //删除MAP所有元素
	   this.clear = function() {
	       this.elements = new Array();
	   };
	   //向MAP中增加元素（key, value)
	   this.put = function(_key, _value) {
	       if(this.containsKey(_key)){
	        this.remove(_key);
	        this.elements.push( {
	           key : _key,
	           value : _value
	        });
	       }else{
	        this.elements.push( {
	           key : _key,
	           value : _value
	        });
	       }  
	   };
	   //删除指定KEY的元素，成功返回True，失败返回False
	   this.remove = function(_key) {
	       var bln = false;
	       try {
	           for (var i = 0; i < this.elements.length; i++) {
	               if (this.elements[i].key == _key) {
	                   this.elements.splice(i, 1);
	                   return true;
	               }
	           }
	       } catch (e) {
	           bln = false;
	       }
	       return bln;
	   };
	   //获取指定KEY的元素值VALUE，失败返回NULL
	   this.get = function(_key) {
	       try {
	           for (var i = 0; i < this.elements.length; i++) {
	               if (this.elements[i].key == _key) {
	                   return this.elements[i].value;
	               }
	           }
	       } catch (e) {
	           return false;
	       }
	       return false;
	   };
	   //判断MAP中是否含有指定KEY的元素
	   this.containsKey = function(_key) {
	       var bln = false;
	       try {
	           for (var i = 0; i < this.elements.length; i++) {
	               if (this.elements[i].key == _key) {
	                   bln = true;
	               }
	           }
	       } catch (e) {
	           bln = false;
	       }
	       return bln;
	   };
	  //获取map的所有values
	  this.values = function() {
	       var arr = new Array();
	       for (var i = 0; i < this.elements.length; i++) {
	           arr.push(this.elements[i].value);
	       }
	       return arr;
	   };
	  //获取map的所有keys
	  this.keys= function() {
	       var arr = new Array();
	       for (var i = 0; i < this.elements.length; i++) {
	           arr.push(this.elements[i].key);
	       }
	       return arr;
	   };
	   //获取数组
	   this.getArray= function(){
	   	var arr = new Array();
	   	for (var i = 0; i < this.elements.length; i++) {
	           arr.push({
		            key : this.elements[i].key,
		            value : this.elements[i].value
		         });
	       }
	       return arr;
	   };
	}
//********************************************
	$(document).ready(function() {
		loadPrivilegeBranchSelect();//加载机构下拉框
		querycheckcontacts()
	})
		//打开合作伙伴 窗口
	function openChannelInfoDiv(pageIndex,obj){
		if(($.trim($('#privilegeBranch').val())).length==0){
			$.message('error',"请先选择机构");
			return;
		}
		$('#addOrEdit').val("");
		$('#channelCode').val('');
		$('#channelName').val('');
		$("#"+obj.id).blur();
		queryTradPartners(pageIndex);
	}
	
	//打开合作伙伴 窗口
	function openChannelInfoDiv1(pageIndex,obj){
		if(($.trim($('#deptCode').val())).length==0){
			$.message('error',"请先选择机构");
			return;
		}
		$('#addOrEdit').val("add");
		$('#channelCode').val('');
		$('#channelName').val('');
		$("#"+obj.id).blur();
		queryTradPartners1(pageIndex);
	}
	//查询新增时合作伙伴
	function queryTradPartners1(pageIndex){
		var size = 8;
		var parentDeptCode = $.trim($('#deptCode').val());
		var channelCode = $.trim($('#channelCode').val());
		var channelName = $.trim($('#channelName').val());
		
		$.openLoading();
		$.ajax({
			url : "<%=path%>/lifePolicyList/queryChannelInfoOfLife",
			type : 'POST',
			async : true,
			data:{
				pageIndex:pageIndex,
				size:size,
				parentDeptCode:parentDeptCode,
				channelCode:channelCode,
				channelName:channelName
			},
			dataType:'json',
			success:function(page){
				$.closeLoading();
				if($.validate(page))return;
				
				var radioObj ="<input class='input_fxk' name='partnerradio' type='radio' value=''>";
				var fieldNames = "channelCode,channelName";
				var tableContent = creatTable1(fieldNames,page,radioObj);
				var tableObj = $("#channelInfoTable");
				tableObj.find("tbody").remove();
				tableObj.append(tableContent);
				
				var strPage = creatChannelPage2(pageIndex,6,page.maxPage,"16");
				var pageDIV = $("#channelInfoPageDIV");
				pageDIV[0].innerHTML="";
				pageDIV.append(strPage);
				
				$('#channelInfoDiv').OpenDiv2();
				
				bindradio();
			}
		});
	}
	//查询 合作伙伴
	function queryTradPartners(pageIndex){
		var size = 8;
		var parentDeptCode = $.trim($('#privilegeBranch').val());
		var channelCode = $.trim($('#channelCode').val());
		var channelName = $.trim($('#channelName').val());
		
		$.openLoading();
		$.ajax({
			url : "<%=path%>/lifePolicyList/queryChannelInfoOfLife",
			type : 'POST',
			async : true,
			data:{
				pageIndex:pageIndex,
				size:size,
				parentDeptCode:parentDeptCode,
				channelCode:channelCode,
				channelName:channelName
			},
			dataType:'json',
			success:function(page){
				$.closeLoading();
				if($.validate(page))return;
				
				var radioObj ="<input class='input_fxk' name='partnerradio' type='radio' value=''>";
				var fieldNames = "channelCode,channelName";
				var tableContent = creatTable1(fieldNames,page,radioObj);
				var tableObj = $("#channelInfoTable");
				tableObj.find("tbody").remove();
				tableObj.append(tableContent);
				
				var strPage = creatChannelPage2(pageIndex,6,page.maxPage,"16");
				var pageDIV = $("#channelInfoPageDIV");
				pageDIV[0].innerHTML="";
				pageDIV.append(strPage);
				
				$('#channelInfoDiv').OpenDiv();
				
				bindradio();
			}
		});
	}
//加载机构下拉框
	function loadPrivilegeBranchSelect(){
		$.ajax({
				url : "<%=path%>/dataLinkManage/queryPrivilegeBranch?"+new Date().getTime(),
				type : 'POST',
				async : false,
				dataType:'json',
				success : function(result) {
					if(result){
						if($.validate(result))return;

						//接口下拉框对象
						var privilegeBranchSelect = document.getElementById("privilegeBranch");
						var deptCodeselect = document.getElementById("deptCode");

						privilegeBranchSelect.length=0;
						
						var data = result.data;
						if(data.length > 0){
							defaultBranch = data[0].deptCode;
								
							for (var i = 0; i < data.length; i++) {
								var option = new Option();
								option.text = data[i].deptCname;
								option.title = data[i].deptCname;
								option.value = data[i].deptCode;
								privilegeBranchSelect.add(option);
							}
							for (var i = 0; i < data.length; i++) {
								var option = new Option();
								option.text = data[i].deptCname;
								option.title = data[i].deptCname;
								option.value = data[i].deptCode;
								deptCodeselect.add(option);
							}
						}
					}
				}
			});
		 }
	//弹出层表格绑定单选按钮事件，选中某个单选按钮，该按钮所在行高亮显示
	function bindradio(){
		$("input[name='partnerradio']").click(function(){
			$("input[name='partnerradio']").parent().parent().removeClass("table_highlighter");
			$(this).parent().parent().addClass("table_highlighter");
	   	});
	}
	//点击“确定”按钮，选择合作伙伴
	function choosePartner(){
		var selectedPartner = $("input[name='partnerradio']:checked");
		if(selectedPartner.length==0){
			$.message("error", "请选择一个合作伙伴");
			return;
		}
		var selectedPartnerNo = selectedPartner.parent().parent().children(" td:eq(1)").html();
		var selectedPartnerName = selectedPartner.parent().parent().children(" td:eq(2)").html();
		var id=$('#addOrEdit').val();
		if(id){
			$('#partnerNo').val(selectedPartnerNo);
			$('#partnerName').val(selectedPartnerName);
		}else{
			$('#searchpartnerNo').val(selectedPartnerNo);
		
		}
		closeCurrentDIV('channelInfoDiv');
	}
	//统一 关闭窗口(传入要关闭DIV的id)
	function closeCurrentDIV(obj){
			if('channelInfoDiv'==obj){
				var id=$('#addOrEdit').val();
				if(id=="add"){
					$("#"+obj).CloseDiv2();	
				}else{
					$("#"+obj).CloseDiv();	
				}
			}else{
				$("#"+obj).CloseDiv();	
			}
			
	}
	//点击“查询”
	function querycheckcontacts(pageIndex){
		if(pageIndex == null){
			pageIndex = 1;
		}
		var privilegeBranch = $.trim($("#privilegeBranch").val());//机构
		var partnerNo = $.trim($("#searchpartnerNo").val());//合作伙伴
		var email = $.trim($("#searchEmail").val());//收件人邮箱
		$.openLoading();
		$.ajax({
			url : "<%=path%>/dataLinkManage/queryTradCheckPageByCriteria",
			type : 'POST',
			dataType:'json',
			data:{
				privilegeBranch:privilegeBranch,
				partnerNo:partnerNo,
				email:email
			},
			success:function(page){
				$.closeLoading();
				if($.validate(page))return;
																															
				var firstbutton ="<a href='javascript:void(0)' onClick='editlinkManager(this);return false'>【编辑】</a><a href='javascript:void(0)' onClick='deletelinkManager(this);return false'>【删除】</a>";
				var fieldNames = "deptName,channelName,receivEmail,copyEmail,channelCode@TDH,deptCode@TDH";
				var tableContent = creatTable1(fieldNames,page,null,firstbutton);																			
				var tableObj = $("#checkcontactsTable");
				tableObj.find("tbody").remove();
				tableObj.append(tableContent);
			}
		});
	}
	var addOrEdit="";
	function add(){
		reset()
		 $('#deptCode').removeAttr("disabled")
		$('#partnerBtn').removeAttr("disabled"); 
		addOrEdit='add';
		$('#addlinkManagerDiv').OpenDiv()
	}
	/**打开编辑*/
	function editlinkManager(obj){
		addOrEdit='edit';
		$('#deptCode').attr("disabled","disabled")
		$('#partnerBtn').attr("disabled","disabled");
		var ditTr = $(obj).parent('td').parent('tr')[0];
		var channelName=ditTr.childNodes[1].innerHTML
		var receviEmail=ditTr.childNodes[2].innerHTML
		var copyEmail=ditTr.childNodes[3].innerHTML
		var channelCode=ditTr.childNodes[4].innerHTML
		var dept=ditTr.childNodes[5].innerHTML
		$('#deptCode').val(dept);
		$('#partnerNo').val(channelCode);
		$('#partnerName').val(channelName)
		$('#receivEmail').val(receviEmail)
		$('#copyEmail').val(copyEmail)
		$('#addlinkManagerDiv').OpenDiv()
	}
	function deletelinkManager(obj){
		$.dialog({
			type: "warn",
			content: "您确定要删除该联系人吗？",
			onOk: function() {
				$.openLoading();
				var ditTr = $(obj).parent('td').parent('tr')[0];
				var channelCode=ditTr.childNodes[4].innerHTML
				var dept=ditTr.childNodes[5].innerHTML
				$.ajax({
					url : "<%=path%>/dataLinkManage/deleteCheckcontacts?"+new Date().getTime(),
					type: "POST",
					async:true,
					data:{
						deptCode:dept,
						partnerNo:channelCode
					},
					dataType: "json",
					success: function(data) {
						$.closeLoading();
						if($.validate(data))return;
						if(data.result=='success'){
							querycheckcontacts();
							closeCurrentDIV('addlinkManagerDiv')
							$.message("success", data.message);
						}else{
							$.message("error", data.message);
						}
					}
				});
			}
		});
	}
	function reset(){
		$('#deptCode').val('');
		$('#partnerNo').val('');
		$('#partnerName').val('')
		$('#receivEmail').val('')
		$('#copyEmail').val('')
	}
	/**保存*/
	function savelinkManager(){
		var deptCode=$('#deptCode').val();
		var partnerNo=$('#partnerNo').val();
		var receivEmail=$('#receivEmail').val();
		var copyEmail=$('#copyEmail').val();
		if(!deptCode){
			$.message("error", "请选择机构");
			return;
		}
		if(!partnerNo){
			$.message("error", "请选择合作伙伴");
			return;
		}
		if(!receivEmail){
			$.message("error", "请填写收件人邮箱地址");
			return;
		}else{
			var emails=receivEmail.split(',');
			var reg=/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/
			for (var i = 0; i < emails.length; i++) {
				var email = emails[i];
				if(!reg.test(email)){
					$.message("error", "收件人邮箱地址填写有误");
					return;
				}
			}
		}
		saveLinkManage(addOrEdit);
	}
	function  saveLinkManage(flag){
		var deptCode=$('#deptCode').val();
		var partnerNo=$('#partnerNo').val();
		var partnerName=$('#partnerName').val();
		var receivEmail=$('#receivEmail').val();
		var copyEmail=$('#copyEmail').val();
		$.openLoading();
		$.ajax({
			url : "<%=path%>/dataLinkManage/saveOrUpdateTradCheck",
			type : 'POST',
			dataType:'json',
			data:{
				flag:flag,
				partnerNo:partnerNo,
				receivEmail:receivEmail,
				copyEmail:copyEmail,
				partnerName:partnerName,
				deptCode:deptCode
			},
			success:function(data){
				$.closeLoading();
				if(data.result=='success'){
					querycheckcontacts();
					closeCurrentDIV('addlinkManagerDiv')
					$.message("success", data.message);
				}else{
					$.message("error", data.message);
				}
			}
		});
	}
	function vreset (){
		$('#searchpartnerNo').val('');
		$('#searchEmail').val('');
	}
	</script>
</head>
<body >
	<div class="current">
           <table class="current_table_1">
              <tbody>
                <tr class="f2">
                  <td>
                    <img src="./images/current_1.jpg">
                  </td>
                  <td class="current_table_1_td">
                    <img src="images/current_1.png">&nbsp;您现在的位置：&nbsp;
                      <span style=" color:#136bab; ">统计分析
                      <span style="padding:0 5px; font-weight:bold; line-height:28px;">&gt;</span>对账联系人管理
                  </span>
                  </td>
                  <td><img src="./images/current_3.jpg"></td>
              </tr>
           </tbody>
         </table>
      </div>
		
		<table class="table_02" id="table">
			<tr>
				<td>
					<table class="search_table">
						<tr height="30px">
							<td style="padding-left:40px; border: 0;text-align: right" nowrap="nowrap">机构：</td>
								<td style="border: 0;text-align: left;padding-right: 70px">
								   <select class="search_select_1" name="privilegeBranch" id="privilegeBranch" onchange="clearBranchRelaField()" style="width: 106px"></select>
								</td>
							<td style="border: 0;text-align: right" nowrap="nowrap">合作伙伴：</td>
		                        <td style="border: 0 ;text-align: left; padding-right: 70px"><input name="partnerNo" id="searchpartnerNo" readonly="readonly" onselect="" style=" background-color: #ccc;" class="input_text">&nbsp;&nbsp;<input type="button"  class="input_button_5"  id="searchPartnerBtn" value="请选择" onClick="openChannelInfoDiv(1,this)"/></td>
							<td style="border: 0;text-align: right" nowrap="nowrap">收件人邮箱地址：</td>
		                        <td style="border: 0 ;text-align: left; padding-right: 70px">
		                        	<input name="partnerNo" id="searchEmail"  onselect=""  class="input_text">&nbsp;&nbsp;</td>
							
							<td style="text-align: center;" colspan="5" width="10%" height="30">
								<input type="button" class="input_button_1" id="searchBtn" value="查询" onClick="querycheckcontacts(1)" />&nbsp;
							    <input type="button" class="input_button_1" id="searchBtn" value="重置" onClick="vreset()" />
							</td>
						</tr>
						<tr height="30px">
						</tr> 
					</table>
				</td>
			</tr>
			<tr>
				<td>
					<table class="search_table">
						<tr height="30px">
							<td width="100%" height="30">
								<input type="button" class="input_button_2"   id="" value="新增收件人" onClick="add()" />&nbsp;
							</td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td>
					<table id="checkcontactsTable" class="content_table_1">
						<thead>
							<tr class="f1">
								<th>机构</th>
								<th>合作伙伴</th>
								<th>收件人邮箱地址</th>
								<th>抄件人邮箱地址</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
						</tbody>
					</table>
					<div id="checkcontactspageDIV" class="page"></div>
				</td>
			</tr>
		</table>
		<!--选择 添加、编辑对账联系人-->
	  	<div id="addlinkManagerDiv" style="display:none;width:50%;">
			<div class="table_title_div">
				<table class="table_title" style='width:100%;height: 20px;'>
					<tr>
					<td width="10%"></td>
					<td width="80%" id="interfaceName"></td>
					<td width="10%" align="right"><img src="./images/closedown.gif" alt="关闭"  onclick="closeCurrentDIV('addlinkManagerDiv')"/></td>
					</tr>
				</table>
			</div>
			<table align="center" id="addtempletTable" class="table_02">
				<tbody>
					<tr>
						<td>
							<table style="height:120px;" class="content_table_1" id="coverageTable">
								
								<tbody>
									<tr>
					                  <td width="40%" style="text-align:right;border-right:0px;">机构：&nbsp;&nbsp;</td>
					                  <td width="60%" style="text-align:left"><select id="deptCode" class="search_select_1" style="width: 106px" onchange="" name="privilegeBranch"></select><span class="red">*</span></td>
					                </tr>
					                <tr>
					                  <td width="40%" style="text-align:right;border-right:0px;">合作伙伴：&nbsp;&nbsp;</td>
										<td style="border: 0 ;text-align: left; padding-right: 70px">
											<input id="partnerNo" class="input_text" style=" background-color: #ccc;" onselect="" readonly="readonly" name="partnerNo">
											<input id="partnerName" class="input_text" style="display: none"  readonly="readonly" name="partnerName">
											<input id="partnerBtn" class="input_button_5" type="button" onclick="openChannelInfoDiv1(1,this)" value="请选择">
											<span class="red">*</span>
										</td>					                
									</tr>
									<tr>
					                  <td width="40%" style="text-align:right;border-right:0px;">收件人邮箱地址：&nbsp;&nbsp;</td>
										<td style="border: 0 ;text-align: left; padding-right: 70px">
											<textarea  rows="3" cols="3" id="receivEmail" class="input_text"  onselect="" style="margin: 0px; width: 161px; height: 103px;"></textarea><span class="red" style="float:right; ">*如需填写邮箱<br>地址;用英文[,]<br>进行分隔</span>
										</td>					                
									</tr>
									<tr>
					                  <td width="40%" style="text-align:right;border-right:0px;">抄件人邮箱地址：&nbsp;&nbsp;</td>
										<td style="border: 0 ;text-align: left; padding-right: 70px">
											<textarea rows="3" id="copyEmail" class="input_text"  onselect=""style="margin: 0px; width: 161px; height: 103px;"></textarea><span class="red" style="float:right; ">如需填写邮箱<br>地址;用英文[,]<br>进行分隔</span>
										</td>					                
									</tr>
								</tbody>
							</table>
							
						</td>
					</tr>
				</tbody>
			</table>
			<div align="center" >
			  <input type="button" id="saveRoleBtn" value="确定" onclick="savelinkManager()" class="input_button_3"/>&nbsp;&nbsp;&nbsp;
			  <input type="button" value="取消" onclick="closeCurrentDIV('addlinkManagerDiv')" class="input_button_3"/>
			</div>
		</div>
		   <!--选择 合作伙伴 div 默认隐藏-->
  	<div id="channelInfoDiv" style="display:none;width:80%;">
		<div class="table_title_div">
			<table class="table_title" style='width:100%;height: 20px;'>
				<tr>
				<td width="10%"></td>
				<td width="80%" id="interfaceName"></td>
				<td width="10%" align="right"><img src="./images/closedown.gif" alt="关闭"  onclick="closeCurrentDIV('channelInfoDiv')"/></td>
				</tr>
			</table>
		</div>
		<table class="table_02" id="table" style="height:180px;" align="center">
			<tr>
				<td>
					<table class="search_table">
						<tr height="30px">
							<td colspan="3" style="text-align:center">
							 合作伙伴代码：<input name="txt3"  id="channelCode"  type="text" class="input_text" value="" >&nbsp;&nbsp;
							 合作伙伴名称：<input name="txt3" id="channelName"  type="text" class="input_text" value="">&nbsp;&nbsp;
								       <input type="button"  class="input_button_1" id="searchBtn" value="查询" onClick="queryTradPartners(1)"/>
							</td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td>
					<table id="channelInfoTable"  class="content_table_1">
						<thead>
							<tr class="f1">
			                  <th style="width:5%">选择</th>
			                  <th style="width:10%">合作伙伴代码</th>
			                  <th style="width:20%">合作伙伴名称</th>
			              	</tr>
						</thead>
						<tbody>
						</tbody>
					</table>
					<div id="channelInfoPageDIV"  class="page"></div>
				</td>
			</tr>
		</table>
		<div align="center" >
		  <input type="button" id="saveRoleBtn" value="确定" onclick="choosePartner()" class="input_button_3"/>&nbsp;&nbsp;&nbsp;
		  <input type="button" value="取消" onclick="closeCurrentDIV('channelInfoDiv')" class="input_button_3"/>
		</div>
		<input type="hidden" id='addOrEdit'/>
	</div>
	</body>
</html>